home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GXFIXED.H < prev    next >
C/C++ Source or Header  |  1993-05-13  |  5KB  |  112 lines

  1. /* Copyright (C) 1989, 1990, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxfixed.h */
  20. /* Fixed-point arithmetic for Ghostscript */
  21.  
  22. #ifndef gxfixed_INCLUDED
  23. #  define gxfixed_INCLUDED
  24.  
  25. /*
  26.  * Coordinates are generally represented internally by fixed-point
  27.  * quantities: integers lose accuracy in crucial places,
  28.  * and floating point arithmetic is slow.
  29.  */
  30. typedef long fixed;
  31. #define max_fixed max_long
  32. #define min_fixed min_long
  33. /*
  34.  * 12 bits of fraction provides both the necessary accuracy and
  35.  * a sufficiently large range of coordinates.
  36.  */
  37. #define _fixed_shift 12
  38. #define _fixed_scale (1<<_fixed_shift)
  39. #define _fixed_rshift(x) arith_rshift(x,_fixed_shift)
  40. #define _fixed_round_v (_fixed_scale>>1)
  41. #define _fixed_fraction_v (_fixed_scale-1)
  42.  
  43. /*
  44.  * Most operations can be done directly on fixed-point quantities:
  45.  * addition, subtraction, shifting, multiplication or division by
  46.  * (integer) constants; assignment, assignment with zero;
  47.  * comparison, comparison against zero.
  48.  * Multiplication and division by floats is OK if the result is
  49.  * explicitly cast back to fixed.
  50.  * Conversion to and from int and float types must be done explicitly.
  51.  * Note that if we are casting a fixed to a float in a context where
  52.  * only ratios and not actual values are involved, we don't need to take
  53.  * the scale factor into account: we can simply cast to float directly.
  54.  */
  55. #define int2fixed(i) ((fixed)(i)<<_fixed_shift)
  56. /* Define some useful constants. */
  57. #define fixed_0 int2fixed(0)
  58. #define fixed_1 int2fixed(1)
  59. #define fixed_half (fixed_1 >> 1)
  60. /*
  61.  * On 16-bit systems, we can convert fixed variables to ints more efficiently
  62.  * than general fixed quantities.  For this reason, we define two separate
  63.  * sets of conversion macros.
  64.  */
  65. #define fixed2int(x) ((int)_fixed_rshift(x))
  66. #define fixed2int_rounded(x) ((int)_fixed_rshift((x)+_fixed_round_v))
  67. #define fixed2int_ceiling(x) ((int)_fixed_rshift((x)+_fixed_fraction_v))
  68. #if arch_ints_are_short & !arch_is_big_endian
  69. /* Do some of the shifting and extraction ourselves. */
  70. #  define _fixed_hi(x) *((uint *)&(x)+1)
  71. #  define _fixed_lo(x) *((uint *)&(x))
  72. #  define fixed2int_var(x)\
  73.     ((int)((_fixed_hi(x) << (16-_fixed_shift)) +\
  74.            (_fixed_lo(x) >> _fixed_shift)))
  75. #  define fixed2int_var_rounded(x)\
  76.     ((int)((_fixed_hi(x) << (16-_fixed_shift)) +\
  77.            (((_fixed_lo(x) >> (_fixed_shift-1))+1)>>1)))
  78. #  define fixed2int_var_ceiling(x)\
  79.     (fixed2int_var(x) -\
  80.      arith_rshift((int)-(_fixed_lo(x) & _fixed_fraction_v), _fixed_shift))
  81. #else
  82. /* Use reasonable definitions. */
  83. #  define fixed2int_var(x) fixed2int(x)
  84. #  define fixed2int_var_rounded(x) fixed2int_rounded(x)
  85. #  define fixed2int_var_ceiling(x) fixed2int_ceiling(x)
  86. #endif
  87. #define fixed2long(x) ((long)_fixed_rshift(x))
  88. #define fixed2long_rounded(x) ((long)_fixed_rshift((x)+_fixed_round_v))
  89. #define fixed2long_ceiling(x) ((long)_fixed_rshift((x)+_fixed_fraction_v))
  90. #define float2fixed(f) ((fixed)((f)*(float)_fixed_scale))
  91. #define fixed2float(x) ((float)((x)*(1.0/_fixed_scale)))
  92.  
  93. /* Rounding and truncation on fixeds */
  94. #define fixed_floor(x) ((x)&(-1L<<_fixed_shift))
  95. #define fixed_rounded(x) (((x)+_fixed_round_v)&(-1L<<_fixed_shift))
  96. #define fixed_ceiling(x) (((x)+_fixed_fraction_v)&(-1L<<_fixed_shift))
  97. #define fixed_fraction(x) ((int)(x)&_fixed_fraction_v)
  98. /* I don't see how to do truncation towards 0 so easily.... */
  99. #define fixed_truncated(x) ((x) < 0 ? fixed_ceiling(x) : fixed_floor(x))
  100.  
  101. /* A point with fixed coordinates */
  102. typedef struct gs_fixed_point_s {
  103.     fixed x, y;
  104. } gs_fixed_point;
  105.  
  106. /* A rectangle with fixed coordinates */
  107. typedef struct gs_fixed_rect_s {
  108.     gs_fixed_point p, q;
  109. } gs_fixed_rect;
  110.  
  111. #endif                    /* gxfixed_INCLUDED */
  112.